import { Component, OnInit, ViewChild, Injector, AfterViewInit, ViewEncapsulation, ElementRef } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { SelectItem } from 'primeng/api';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator, LazyLoadEvent } from 'primeng/primeng';
//import { ExcelModalComponent } from '@app/shared/common/excel-modal/excel-modal.component';
import * as moment from 'moment';
import { BsDatepickerViewMode } from 'ngx-bootstrap/datepicker/models';
import { BsDatepickerConfig, BsDaterangepickerConfig } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import * as G2 from '@antv/g2';
import { PrimengTableHelper } from '@shared/helpers/PrimengTableHelper';
import { LineInstanceServiceProxy, TaskLineState, ComboboxItemDto, TemplateServiceProxy, AddRequestInput, WorkingTeamProportion, WorkingTeamServiceProxy } from '@shared/service-proxies/service-proxies';
import { ActivatedRoute } from '@angular/router';

@Component({
  templateUrl: './process.component.html',
  styleUrls: ['./process.component.css'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]

})
export class ProcessComponent extends AppComponentBase  implements OnInit {
 

  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginatorNormal') paginatorNormal: Paginator;
  @ViewChild('dataTableNormalProductions') dataTableNormalProductions: Table;

  advancedFiltersAreShown: Boolean = false;

  primengTableHelperNormalReport = new PrimengTableHelper();
  paginatorReport = new Paginator();  

  filterTaskModel: string = '';  
  filterProductionDrawingCode: string = '';
  filterWorkStation = '';
  requestWorkStationName = '';
  filterState = '';
  filterWorkingTeam = '';

  workStations: ComboboxItemDto[] = [];
  states: ComboboxItemDto[]= [];
  workingTeams : WorkingTeamProportion[]= [];

  constructor(
    injector: Injector,
    private _lineInstanceServiceProxy :LineInstanceServiceProxy,
    private _templateServiceProxy :TemplateServiceProxy,
    private activateInfo: ActivatedRoute,
    private _workingTeamAppService: WorkingTeamServiceProxy,
  ) {
    super(injector);    

    this.activateInfo.queryParams.subscribe(params =>{
      this.filterTaskModel=params.model;
      this.requestWorkStationName = params.station;
      });

    //初始化工位列表
     this._templateServiceProxy.getAllStations()
      .subscribe((result)=>
        {
          result.forEach(station => {
            let cstation = new ComboboxItemDto();
            cstation.value = station.id;
            cstation.displayText = station.stationName;
            this.workStations.push(cstation);
            if(cstation.displayText ==  this.requestWorkStationName)
            {             
               this.filterWorkStation =  cstation.value;
               this.getReport();
            }
          })
        }
        );

    //初始化状态列表   
      
      for (const [key, value] of Object.entries(TaskLineState)) {
        if (!Number.isNaN(Number(key))) {
            continue;
        }
        let cState= new ComboboxItemDto();
        cState.value = key;
        cState.displayText = key;

        this.states.push(cState);
    }
    //初始化班组列表
    this._workingTeamAppService.getProportions()
    .subscribe((result)=>
      {
        this.workingTeams = result;
      }
    );

  }

  ngOnInit(): void {
  }


  getReport(event?: LazyLoadEvent) {
  

    this.primengTableHelperNormalReport.showLoadingIndicator();  


    this._lineInstanceServiceProxy.getAllLineInstance(
      this.filterTaskModel,
      this.filterProductionDrawingCode,
      this.filterWorkStation,
      this.filterState,
      this.filterWorkingTeam,
      this.primengTableHelperNormalReport.getSorting(this.dataTableNormalProductions),
      this.primengTableHelperNormalReport.getMaxResultCount(this.paginatorNormal, event),
      this.primengTableHelperNormalReport.getSkipCount(this.paginatorNormal, event)
    )
    .subscribe((result) => {
      this.primengTableHelperNormalReport.totalRecordsCount = result.totalCount;
      this.primengTableHelperNormalReport.records = result.items;
      this.primengTableHelperNormalReport.hideLoadingIndicator();
    });
  }

  print(qrcode:string,drawingCode:string,quantity:string,specialInfo:string) {
    this._lineInstanceServiceProxy.printInstanceQr(qrcode,drawingCode,quantity,specialInfo).subscribe(()=>{
    this.notify.info('打印成功！');
    });
  }

  addRequestion(instanceId:string) {

    let addRequest = new AddRequestInput();
    addRequest.instanceId = instanceId;
    addRequest.requestInfo = '测试问题';
    this._lineInstanceServiceProxy.addRequest(addRequest).subscribe(()=>{
      this.notify.info('申请故障');
      this.getReport();
    });
  }

}